package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class S_FixWeaponList extends ServerBasePacket {
  private byte[] _byte;
  
  public S_FixWeaponList(List<L1ItemInstance> weaponList) {
    this._byte = null;
    writeC(83);
    writeD(200);
    writeH(weaponList.size());
    Iterator<L1ItemInstance> iterator = weaponList.iterator();
    while (iterator.hasNext()) {
      L1ItemInstance weapon = iterator.next();
      writeD(weapon.getId());
      writeC(weapon.get_durability());
    } 
  }
  
  public S_FixWeaponList(L1PcInstance pc) {
    this._byte = null;
    buildPacket(pc);
  }
  
  private void buildPacket(L1PcInstance pc) {
    writeC(83);
    writeD(200);
    List<L1ItemInstance> weaponList = new ArrayList<>();
    List<L1ItemInstance> itemList = pc.getInventory().getItems();
    Iterator<L1ItemInstance> iterator = itemList.iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      switch (item.getItem().getType2()) {
        case 1:
          if (item.get_durability() <= 0)
            continue; 
          weaponList.add(item);
      } 
    } 
    writeH(weaponList.size());
    Iterator<L1ItemInstance> iterator2 = weaponList.iterator();
    while (iterator2.hasNext()) {
      L1ItemInstance weapon = iterator2.next();
      writeD(weapon.getId());
      writeC(weapon.get_durability());
    } 
  }
  
  public S_FixWeaponList(L1ItemInstance weapon) {
    this._byte = null;
    writeC(83);
    writeD(200);
    writeH(1);
    writeD(weapon.getId());
    writeC(weapon.get_durability());
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
